home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Collections: Camelot
/
Camelot 098 (1990-12)(Swedish User Group of Amiga)(SE)(PD)[WB].zip
/
Camelot 098 (1990-12)(Swedish User Group of Amiga)(SE)(PD)[WB].adf
/
XLisp-Stat
/
Functions
/
robustregression.lsp
< prev
next >
Wrap
Lisp/Scheme
|
1990-10-11
|
2KB
|
43 lines
; book pp.174-176
(defun robust-coefs (x y wf &key (weights (repeat 1 (length y)))
(tol .0001)
(count-limit 20))
(let ((x (if (matrixp x) x (apply #'bind-columns x))))
(labels ((as-list (x) (coerce (compound-data-seq x) 'list))
(rel-err (x y)
(mean (/ (abs (- x y)) (+ 1 (abs x)))))
(reg-coefs (weights)
(let* ((m (make-sweep-matrix x y weights))
(p (array-dimension x 1)))
(as-list
(select (first (sweep-operator m (iseq 1 p)))
(1+ p)
(iseq 0 p)))))
(fitvals (beta)
(+ (first beta)(matmult x (rest beta))))
(improve-guess (beta)
(let* ((resids (- y (fitvals beta)))
(scale (/ (median (abs resids)) .6745))
(wts (funcall wf (/ resids scale))))
(reg-coefs wts)))
(good-enough-p (last beta count)
(if (> count count-limit)
(format t "Iteration limit exceeded ~%"))
(or (> count count-limit)
(and last (< (rel-err beta last) tol)))))
(do ((last nil beta)
(count 0 (+ count 1))
(beta (reg-coefs weights)(improve-guess beta)))
((good-enough-p last beta count) beta)))))
(defun make-wf (name &optional (k (case name (biweight 4.685)
(cauchy 2.385)
(huber 1.345))))
#'(lambda (r)
(let ((u (abs (/ r k))))
(case name
(biweight (^ (- 1 (^ (pmin u 1) 2)) 2))
(cauchy (/ 1 (+ 1 (^ u 2))))
(huber (/ 1 (pmax u 1)))))))